import Vue from 'vue'
import VueRouter from 'vue-router'
import { baseRoutes } from './modules/base'
import { demoRoutes } from './modules/demo'
import { getToken } from '@/utils/token'

Vue.use(VueRouter)

// 免登录白名单
const whiteList = ['/login']

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  // 返回页面时，滚动到顶
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    ...baseRoutes, ...demoRoutes
  ]
})

/* 路由前置守卫 */
router.beforeEach((to, from, next) => {
  // 开发环境
  if (process.env.NODE_ENV === 'development') {
    next()
    return
  }
  // 生产环境
  if (whiteList.indexOf(to.path) > -1) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      next('/login')
    }
  }
})

/* 路由后置守卫 */
router.afterEach((to, from) => {
})

export default router
